package com.sduept.nwld.dataserver.manager.fault;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.entity.FaultSourceUploadInfo;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.fault.analysis.model.FaultUploadInfoDTO;
import com.sduept.nwld.fault.analysis.model.StationFaultUploadInfoDTO;
import com.sduept.utils.SpringContext;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//@Service
//@TransformFault
public class TransformerFaultUploadTransform implements IFaultUploadTransform {

//	@Autowired
//	private FaultRecordQueryManager fqm;
	FaultRecordQueryManager fqm = SpringContext.getBean(FaultRecordQueryManager.class);
	
	@Override
	public List<FaultUploadInfoDTO> transform(List<FaultSourceUploadInfo> infos) {
		List<FaultUploadInfoDTO> list = new ArrayList<FaultUploadInfoDTO>();
		Map<String, List<FaultSourceUploadInfo>> groupMap = infos.stream().collect(Collectors.groupingBy(FaultSourceUploadInfo::getFaultId));
		for (String faultId : groupMap.keySet()) {
			List<FaultSourceUploadInfo> lt = groupMap.get(faultId);
			FaultUploadInfoDTO dto = genFaultUploadInfoDTOByFault(faultId, lt);
			list.add(dto);
		}
		return list;
	}

	/**
	 * 获取FaultUploadInfoDTO
	 * @param fault
	 * @param list
	 * @return
	 */
	private FaultUploadInfoDTO genFaultUploadInfoDTOByFault(String faultId, List<FaultSourceUploadInfo> list) {
		FaultRecord fault = fqm.findById(faultId);
		FaultUploadInfoDTO dto = setAttributesByFault(fault);
		// 获取FaultUploadInfoDTO的StationFaultUploadInfoDTO
		StationFaultUploadInfoDTO stationInfo = genStationFaultUploadInfoDTO(fault,list);
		dto.getStationInfo().add(stationInfo);
		return dto;
	}
	
	/**
	 * 根据变压器故障,设置FaultUploadInfoDTO的属性
	 * @param fr
	 * @return
	 */
	private FaultUploadInfoDTO setAttributesByFault(FaultRecord fr){
		FaultUploadInfoDTO result = new FaultUploadInfoDTO();
		result.setId(ObjectId.get().toString());
		result.setRecordId(fr.getId());
		result.setEtype(fr.getEtype());
		result.setFaultDesc(fr.getEquipment()+"故障");
		result.setFaultDevice(fr.getEquipment());
		result.setFaultTime(fr.getFaultTime());
		return result;
	}
	
	/**
	 * 获取List<StationFaultUploadInfoDTO>
	 * @param valueMap
	 * @return
	 */
	private StationFaultUploadInfoDTO genStationFaultUploadInfoDTO(FaultRecord fault,List<FaultSourceUploadInfo> list) {
		StationFaultUploadInfoDTO sdto = new StationFaultUploadInfoDTO();
		sdto.setStationName(fault.getStationName());
		genStationFaultUploadInfoDTO(list,sdto);
		return sdto;
	}
	
	/**
	 * 将一个故障中一个变电站对应的多个FaultUploadInfo，封装成StationFaultUploadInfoDTO
	 * @param list
	 * @return
	 */
	private void genStationFaultUploadInfoDTO(List<FaultSourceUploadInfo> list,StationFaultUploadInfoDTO dto) {
		int deviceType = -1; // 设备类型：0：录波器，1：主保护， 2：断路器保护，3：非电量保护
		for (FaultSourceUploadInfo info : list) {
			deviceType = info.getDeviceType();
			if(0 == deviceType) {
				setCenterWaveUploadMethod(info, dto); // 设置集中录波上送方式
			} else if (1 == deviceType) {
				setMainProtectUploadMethod(info, dto); // 设置主保护录波和元件的上送方式
			} else if (2 == deviceType) {
				setBreakerProtectUploadMethod(info, dto); // 设置断路器保护录波和元件的上送方式
			} else if (3 == deviceType) {
				setNonElectricityProtectionUploadMethod(info, dto); // 设置非电量保护录波和元件的上送方式
			}
		}
	}
	
	/**
	 * 设置集中录波的上送方式
	 * @param info
	 * @param dto
	 * @return
	 */
	private void setCenterWaveUploadMethod(FaultSourceUploadInfo info, StationFaultUploadInfoDTO dto) {
		if(0==info.getUploadType() && 0==info.getDeviceType()) {
			dto.setWaveInfo(info.getUploadMethod());
		}
	}
	/**
	 * 设置主保护录波和元件的上送方式
	 * @param info
	 * @param dto
	 * @return
	 */
	private void setMainProtectUploadMethod(FaultSourceUploadInfo info, StationFaultUploadInfoDTO dto) {
		// 主保护录波
		if(0==info.getUploadType() && 1==info.getDeviceType()) {
			if(info.getName().contains("主一")) {
				dto.setMain1WaveInfo(info.getUploadMethod());
			} else if (info.getName().contains("主二")) {
				dto.setMain2WaveInfo(info.getUploadMethod());
			}
		}
		// 主保护元件
		if(1==info.getUploadType() && 1==info.getDeviceType()) {
			if(info.getName().contains("主一")) {
				dto.setMain1DatagramInfo(info.getUploadMethod());
			} else if (info.getName().contains("主二")) {
				dto.setMain2DatagramInfo(info.getUploadMethod());
			}
		}
	}

	/**
	 * 设置断路器保护录波和元件的上送方式
	 * @param info
	 * @param dto
	 * @return
	 */
	private void setBreakerProtectUploadMethod(FaultSourceUploadInfo info, StationFaultUploadInfoDTO dto) {
		// 首先判断断路器是500k还是220kv,如果是220kv，一定写在断路器3的位置
		FaultRecord fault = fqm.findById(info.getFaultId());
		if(220==fault.getVoltage()) {
			// 断路器保护录波
			dto.setBreakerProtect3Name(info.getName());
			if(0==info.getUploadType() && 2==info.getDeviceType()) {
				dto.setBreakerProtect3WaveInfo(info.getUploadMethod());
			}
			// 断路器保护元件
			if(1==info.getUploadType() && 2==info.getDeviceType()) {
				dto.setBreakerProtect3DatagramInfo(info.getUploadMethod());
			}
		} else if (500==fault.getVoltage()) {
			// 断路器保护录波
			if(0==info.getUploadType() && 2==info.getDeviceType()) {
				if(StringUtils.isEmpty(dto.getBreakerProtect1Name()) || info.getName().equals(dto.getBreakerProtect1Name())) {
					dto.setBreakerProtect1Name(info.getName());
					dto.setBreakerProtect1WaveInfo(info.getUploadMethod());
				} else if(StringUtils.isEmpty(dto.getBreakerProtect2Name()) || info.getName().equals(dto.getBreakerProtect2Name())){
					dto.setBreakerProtect2Name(info.getName());
					dto.setBreakerProtect2WaveInfo(info.getUploadMethod());
				}
			}
			// 断路器保护元件
			if(1==info.getUploadType() && 2==info.getDeviceType()) {
				if(StringUtils.isEmpty(dto.getBreakerProtect1Name()) || info.getName().equals(dto.getBreakerProtect1Name())) {
					dto.setBreakerProtect1Name(info.getName());
					dto.setBreakerProtect1DatagramInfo(info.getUploadMethod());
				} else if(StringUtils.isEmpty(dto.getBreakerProtect2Name()) || info.getName().equals(dto.getBreakerProtect2Name())){
					dto.setBreakerProtect2Name(info.getName());
					dto.setBreakerProtect2DatagramInfo(info.getUploadMethod());
				}
			}
		}
	}
	
	/**
	 * 设置非电量保护录波和元件的上送方式
	 * @param info
	 * @param dto
	 */
	private void setNonElectricityProtectionUploadMethod(FaultSourceUploadInfo info, StationFaultUploadInfoDTO dto) {
		// 非电量保护录波
		if(0==info.getUploadType() && 3==info.getDeviceType()) {
			dto.setNonElectricityProtectionWaveInfo(info.getUploadMethod());
		}
		// 非电量保护元件
		if(1==info.getUploadType() && 3==info.getDeviceType()) {
			dto.setNonElectricityProtectionDatagramInfo(info.getUploadMethod());
		}
	}
	
}
